package com.moon.game.command.guest.market;


import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.ItemMG;
import com.moon.game.resource.model.Item;
import com.moon.game.service.ItemService;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.ErrorMsg;

/**
 * @description 出售非装备类道具
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-6-16 下午8:30:19
 */
public class SellItemCmd extends BaseCmd{
	
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}
	}
	
	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null || ps.length < 2) {
			doResponsePsError(req);
			return;
		}
		Integer itemId = Integer.parseInt(ps[0]);
		Integer num = Integer.parseInt(ps[1]);
		if (num == null || (num.intValue() < 1 || num.intValue() > Constants.ONCE_USE_ITEM_MAX_NUM)) {
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_BEYOND_MAX_NUM, "请输入1~" + Constants.ONCE_USE_ITEM_MAX_NUM + "之间的数字！"));
			return;
		}
		if (itemId == null || itemId.intValue() == 0) {
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_PAR, "道具id错误！"));
			return;
		}
		final Item item = ItemMG.instance.getItem(itemId);
		if (item == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_PAR, "获取道具资源失败！"));
			return;
		}
		int price = 100;
		if (item.getMoney() != null && item.getMoney().intValue() != 0) {
			price = item.getMoney() / 2;
		}
		price *= num;
		if (ps.length == 2) {
			req.setVO("item_id", itemId);
			req.setVO("num", num);
			req.setVO("item_name", item.getName());
			req.setVO("price", price);
			doResult(req, "ack");
			return;
		}
		StringBuffer info = new StringBuffer();
		Integer ack = Integer.parseInt(ps[2]);
		if (ack == null || ack != 1) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_LACK_THE_ITEM, "参数错误,出售" + item.getName() + "(×" + num + ")失败！"));
			return;
		}
		int ret = ItemService.delItem(getPlayerVo(req), itemId, num);
		if (ret == Constants.DEL_ITEM_SUCC) {
			info.append("出售 "+ item.getName() +"(×" + num + ")成功，您的碎银增加：+" + price);
			req.setVO(TAG_INFO, info);
			PlayerService.doAddMoney(req.getSid(), req.getPlayerId(), price);
		}else if (ret == Constants.DEL_ITEM_NOT_ENOUGH) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_LACK_THE_ITEM, "您想出售 "+ item.getName() +"(×" + num + "),当前拥有(×" + item.getNum() + "),数量不够,出售失败！"));
			return;
		}else if (ret == Constants.DEL_ITEM_LACK) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_HAVE_NO_THE_ITEM, "您当前没有"+ item.getName() +"！"));
			return;
		}else if (ret == Constants.DEL_ITEM_FAIL) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_ITEM_BASE, "出售"+ item.getName() +"异常！"));
			return;
		}
		response(req, req.getClientResponse());
	}

}
